﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace SJHT_CustomerVoiceDownLoad.Common
{
    public class Config
    {
        public static string ConnStr = ConfigurationManager.AppSettings["ConnStr"];
        public static int DbType = Convert.ToInt32(ConfigurationManager.AppSettings["DbType"]);
        public static string SysIP = ConfigurationManager.AppSettings["SysIP"];
        public static string SysUser = ConfigurationManager.AppSettings["SysUser"];
        public static string SysPwd = ConfigurationManager.AppSettings["SysPwd"];
        public static string SysPath = ConfigurationManager.AppSettings["SysPath"];
        public static Boolean IsLog = Convert.ToBoolean(ConfigurationManager.AppSettings["IsLog"]);
        public static string LogPath = ConfigurationManager.AppSettings["LogPath"];
        public static Boolean IsSendSMS = Convert.ToBoolean(ConfigurationManager.AppSettings["IsSendSMS"]);
        public static string SendSMSPhone = ConfigurationManager.AppSettings["SendSMSPhone"];
        public static string ConnShaWenStr = ConfigurationManager.AppSettings["ConnShaWenStr"];
        public static string SysShaWenIP = ConfigurationManager.AppSettings["SysShaWenIP"];
        public static string SysShaWenUser = ConfigurationManager.AppSettings["SysShaWenUser"];
        public static string SysShaWenPwd = ConfigurationManager.AppSettings["SysShaWenPwd"];
        public static string FileSavePath = ConfigurationManager.AppSettings["FileSavePath"];

        ///<summary> 
        ///更新连接字符串  
        ///</summary> 
        ///<param name="newName">连接字符串名称</param> 
        ///<param name="newConString">连接字符串内容</param> 
        ///<param name="newProviderName">数据提供程序名称</param> 
        public static void UpdateConnectionStringsConfig(string newName, string newConString)
        {

            //获取Configuration对象
            Configuration config = System.Configuration.ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            ////根据Key读取<add>元素的Value
            //string name = config.AppSettings.Settings["name"].Value;
            ////写入<add>元素的Value
            //config.AppSettings.Settings["name"].Value = "fx163";
            ////增加<add>元素
            //config.AppSettings.Settings.Add("url", "http://www.fx163.net");
            ////删除<add>元素
            //config.AppSettings.Settings.Remove("name");
            ////一定要记得保存，写不带参数的config.Save()也可以
            //config.Save(ConfigurationSaveMode.Modified);
            ////刷新，否则程序读取的还是之前的值（可能已装入内存）
            //System.Configuration.ConfigurationManager.RefreshSection("appSettings");

            bool exist = false; //记录该连接串是否已经存在  
            //如果要更改的连接串已经存在  
            if (config.AppSettings.Settings[newName] != null)
            {
                exist = true;
            }
            // 如果连接串已存在，首先删除它  
            if (exist)
            {
                config.AppSettings.Settings.Remove(newName);
            }
            //增加<add>元素
            config.AppSettings.Settings.Add(newName, newConString);
            // 保存对配置文件所作的更改  
            config.Save(ConfigurationSaveMode.Modified);
            // 强制重新载入配置文件的ConnectionStrings配置节  
            System.Configuration.ConfigurationManager.RefreshSection("appSettings");
        }
    }
    /*
    class Config
    {
        //本地录音保存路径
        public static string FileSavePath = Properties.Settings.Default.FileSavePath;

        //数据库类型
        public static int DbType = Properties.Settings.Default.DbType;

        //数据库连接串
        public static string ConnStr = Properties.Settings.Default.ConnStr;
        public static string ConnShaWenStr = Properties.Settings.Default.ConnShaWenStr;
        //服务器IP
        public static string SysIP = Properties.Settings.Default.SysIP;
        public static string SysShaWenIP = Properties.Settings.Default.SysShaWenIP;

        //系统用户
        public static string SysUser = Properties.Settings.Default.SysUser;
        public static string SysShaWenUser = Properties.Settings.Default.SysShaWenUser;

        //系统用户密码
        public static string SysPwd = Properties.Settings.Default.SysPwd;
        public static string SysShaWenPwd = Properties.Settings.Default.SysShaWenPwd;

        //系统路径
        public static string SysPath = Properties.Settings.Default.SysPath;

        //是否写日志
        public static Boolean IsLog = Properties.Settings.Default.IsLog;

        //日志保存路径
        public static string LogPath = Properties.Settings.Default.LogPath;

        //日志保存路径
        public static Boolean IsSendSMS = Properties.Settings.Default.IsSendSMS;

        //日志保存路径
        public static string SendSMSPhone = Properties.Settings.Default.SendSMSPhone;
        
        //配置类初始化
        public static void init()
        {
            FileSavePath = Properties.Settings.Default.FileSavePath;
            DbType = Properties.Settings.Default.DbType; 
            ConnStr = Properties.Settings.Default.ConnStr; 
            SysIP = Properties.Settings.Default.SysIP; 
            SysUser = Properties.Settings.Default.SysUser; 
            SysPwd = Properties.Settings.Default.SysPwd; 
            SysPath = Properties.Settings.Default.SysPath;
            IsLog = Properties.Settings.Default.IsLog; 
            LogPath = Properties.Settings.Default.LogPath;
            IsSendSMS = Properties.Settings.Default.IsSendSMS;
            SendSMSPhone = Properties.Settings.Default.SendSMSPhone;
        }

    }
    */
}
